
module ICarry {

    export class RequestViewModel implements PrismBreak.PbViewModel {

        private _app: ICarryApplication;
        private _view: PrismBreak.View;

        private nextToken: string;
        private previousToken: string;

        private sortType: KnockoutObservable<string>;
        private currentSort: ICarry.SortType;

        private carriers: KnockoutObservableArray<ICarry.Carrier>;


        constructor(app: PrismBreak.PbApp) {
            this._app = <ICarryApplication>app;

        }

        public Initialize(view: PrismBreak.View) {
            this._view = <PrismBreak.View> view;

            this.sortType = ko.observable("0");
            this.sortType.subscribe((newValue: string) => {
                this.CarriersOrderHasChanged(newValue);
            });
            this.currentSort = ICarry.SortType.Default;

            this.carriers = ko.observableArray<ICarry.Carrier>();

            //defaults for now
            this.carriers([
                new Carrier("Michele D.", 1),
                new Carrier("Francisco C.", 3),
                new Carrier("Alfio S.", 1),
                new Carrier("Saro F.", 4),
                new Carrier("Denise H.", 1),
            ]);

            ko.applyBindings(this, view.Node);

        }

        public Activated() {
            //Start listening to next event
            this.nextToken = this._app.EventManager.Subscribe(PrismBreak.EventType[PrismBreak.EventType.RequestNextStep], () => {
                this.GoToNextStep();
            });

            this.previousToken = this._app.EventManager.Subscribe(PrismBreak.EventType[PrismBreak.EventType.RequestPreviousStep], () => {
                this.GoToPreviousStep();
            });

            this._app.EventManager.Publish(ICarry.ICarryEvent[ICarry.ICarryEvent.NextButtonVisibilityChange], false);

        }

        private GoToNextStep(): void {

            if (this.CanGoNext()) {
                //Stop listening to next event
                this._app.EventManager.Unsubscribe(PrismBreak.EventType[PrismBreak.EventType.RequestNextStep], this.nextToken);

                this._app.EventManager.Unsubscribe(PrismBreak.EventType[PrismBreak.EventType.RequestPreviousStep], this.previousToken);

                //activate next view

                this._app.ViewManager.ActivateViewById("CarrierProfileView");

            }
        }

        private CanGoNext(): boolean {
            //TODO: do some validation
            return true;
        }

        private GoToPreviousStep(): void {

            if (this.CanGoPrevious()) {
                //Stop listening to previous event
                this._app.EventManager.Unsubscribe(PrismBreak.EventType[PrismBreak.EventType.RequestNextStep], this.nextToken);
                this._app.EventManager.Unsubscribe(PrismBreak.EventType[PrismBreak.EventType.RequestPreviousStep], this.previousToken);

                this._app.EventManager.Publish(ICarry.ICarryEvent[ICarry.ICarryEvent.NextButtonVisibilityChange], true);

                //activate previous view
                this._app.ViewManager.ActivateViewById("RequestOfferView");


            }
        }

        private CanGoPrevious(): boolean {
            //TODO: do some validation
            return true;
        }

        private CarriersOrderHasChanged(value: string): void {

            if (value === "0") {
                this.currentSort = ICarry.SortType.Default;
            }
            else {
                this.currentSort = ICarry.SortType.Rank;
            }

        }

        private MoreCarriersClickEvent(): void {


        }

        private ExpandCarrier(carrier: Carrier, ev: Event): void {

            this._app.EventManager.Publish(ICarry.ICarryEvent[ICarry.ICarryEvent.SetCarrierInformation], carrier);
            this.GoToNextStep();
        }

    }

    export enum SortType {
        Default,
        Rank
    }

    export class Carrier {

        public Name: KnockoutObservable<string>;
        public Rank: KnockoutObservable<number>;
        public Note: KnockoutObservable<string>;
        public ImageSrc: KnockoutObservable<string>;

        public CarrierInfoEntries: KnockoutObservableArray<ICarry.CarrierInfo>;

        public constructor(name?: string, rank?: number) {
            this.Name = ko.observable(name);
            this.Rank = ko.observable(rank);
            this.Note = ko.observable("This is the personal note for the user " + this.Name());
            this.ImageSrc = ko.observable("../../../Styling/Images/default-user-profile.jpg");

            this.CarrierInfoEntries = ko.observableArray<ICarry.CarrierInfo>();
            //test
            this.CarrierInfoEntries.push(new ICarry.CarrierInfo("Id checked", true));
            this.CarrierInfoEntries.push(new ICarry.CarrierInfo("Cell checked", false));
            this.CarrierInfoEntries.push(new ICarry.CarrierInfo("20 kg exchanged", true));
        }
    }

}   